﻿using CMS.Models;
using CMS.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class UnitTypeViewModel : ViewModelBase
    {
        private List<UnitType> unitTypes = new List<UnitType>();
        public List<UnitType> UnitTypes
        {
            get { return unitTypes; }
            set
            {
                unitTypes = value;
                RaisePropertyChanged();
            }
        }

        public UnitTypeViewModel()
        {
            unitTypes = new UnitTypeProvider().Select();
        }


        public RelayCommand OpenAddUnitTypeWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddUnitTypeWindow();
                    window.ShowDialog();
                    //刷新数据
                    UnitTypes = new UnitTypeProvider().Select();
                });
            }
        }


        public RelayCommand<object> DeleteUnitTypeCommand
        {
            get
            {
                return new RelayCommand<Object>((arg) =>
                {
                    if (!(arg is UnitType model)) return;
                    var cargos = new CargoProvider().Select().FindAll(t => t.UnitId == model.Id);
                    if (cargos != null && cargos.Count > 0)
                    {
                        MessageBox.Show("当前类型已有引用");
                        return;
                    }

                    var count = new UnitTypeProvider().Delete(model);
                    if (count > 0)
                    {
                        AppData.Instance.MainWindow.ShowMessageAsync("提示", "删除完成");
                    }
                    //刷新数据
                    UnitTypes = new UnitTypeProvider().Select();
                });
            }
        }

    } 
}
